

import config from './config.js';
import service from './service.js';
//一开始配置服务器
function setHeader(headerObj = {}, isToken = true) {
	let header = {'content-type': 'application/x-www-form-urlencoded'};
	if(isToken === true) {
		let token = service.getToken();
		header['Token'] = token;
	}else if(typeof isToken == 'string') {
		header['Token'] = isToken;
	}
	header['Current'] = 'YES';
	Object.assign(header, headerObj);
	return header;
}

function showLoading(isShowLoading) {
	//提示框数据
	let loadingObj = {
		title: '',
		mask: true,
		success: null,
		fail: null,
		complete: null
	}
	
	if(isShowLoading === true) {
		uni.showLoading(loadingObj);//调用提示框
	}else if(typeof isShowLoading == 'object') {
		Object.assign(isShowLoading, loadingObj);
		uni.showLoading(loadingObj);
	}
}

function isSetWebsite() {
	let website = service.getWebsite();//服务器地址
	if(website == '' || website.length <= 7) {
		uni.showModal({
			title: '提示',
			content: '未设置服务器地址，是否去设置',
			showCancel: false,
			success: (res) => {
				if (res.confirm) {
					console.log('aaa');
					uni.navigateTo({
						url: 'pages/setting/setting'//重新设置服务器地址
					});
				}
			}
		});
		return false;
	}
	return website;
}

function uniNet(url, data, method, success, fail, complete, isShowLoading = true, isToken, headerObj, isError = true) {
	
	let website = isSetWebsite();
	
	console.log(website+url);
	
	if(!website) return;
	
	showLoading(isShowLoading);
	
	if(website.lastIndexOf('/') == website.length -1) {
		website = website.substring(0, website.length-1);
	}
	
	let netObj = uni.request({
		method: method,
		url: website+url,
		header: setHeader(headerObj, isToken),
		data: data,
		success: (res)=> {
			//console.log(JSON.stringify(res));
			if(res.statusCode == 200) {
				let data = res.data;
				if(data && (data.code == 401 || data.code == 402 || data.code == 403)) {
					// 是否需要错误处理
					if(isError) {
						switch(data.code+''){
							case '401':
								let msgArr = ['用户未登陆','登录已过期','该账号已在其他设备登录'];
								if(msgArr.indexOf(data.msg) > -1) {
									// let title = '未登录', msg = '您的账号已在另一台设备登录，如不是本人操作，为安全起见请尽快修改登录密码。';
									let title = '未登录', msg = '您未登录,请先登录再进行操作。';
									if(data.msg == msgArr[1]){
										title = '';
										msg = '您的账号已在另一台设备登录，如不是本人操作，为安全起见请尽快修改登录密码。';
									}
									if(data.msg == msgArr[2]) {
										title = '';
										msg = msgArr[2];
									}
									uni.showModal({
										title: title,
										content: msg,
										showCancel: false,
										success: (res) => {
											if (res.confirm) {
												service.logout();
												uni.reLaunch({
													url: '../login/login'
												});
											}
										}
									});
								} else {
									if(data.msg) {
										uni.showToast({
											icon: 'none',
											title: data.msg
										});
									}
								}
								break;
								case '403':
									if(data.msg) {
										uni.showToast({
											icon: 'none',
											title: data.msg
										});
									}
									break;
						}
					}
					fail && fail(res);
				} else {
					success && success(data);
					if(data && data.token) {
						service.setToken(data.token);
					}
				}
			} else {
				switch(res.statusCode) {
					case 500: 
						uni.showToast({icon: 'none',title: '服务器异常'});
						break;
				}
			}
			
		},
		fail: (res)=> {
			let msg = '请求失败';
			if(website.indexOf('http://') == -1 || website.indexOf('https://') == -1) {
				msg = '请求失败，请查看服务器地址是否设置正确';
			}
			if(!fail) {
				uni.showToast({
					icon: 'none',
					title: msg
				});
			}
			console.log(JSON.stringify(res));
			fail && fail(res.data);
		},
		complete: (res)=>{//接口调用结束的回调函数（调用成功、失败都会执行）
			console.log('complete');
			setTimeout(()=>{
				uni.hideLoading();
			},2000)
			
			complete && complete(res);
		}
	});
	
	return netObj;
}

function netGet(url, data, success, fail, complete, isShowLoading, isToken, headerObj, isError) {
	return uniNet(url, data, 'GET', success, fail, complete, isShowLoading, isToken, headerObj, isError);
}

function netPost(url, data, success, fail, complete, isShowLoading, isToken, headerObj, isError) {
	return uniNet(url, data, 'POST', success, fail, complete, isShowLoading, isToken, headerObj, isError);
}

// 退出
function logout() {
	uni.reLaunch({
		url: '../login/login'
	});
	netPost('/module/api/main/logout', null, ()=>{
		service.logout();
	}, undefined, undefined, undefined, undefined, undefined, false);
}


export default {
	netGet: netGet,
	netPost: netPost,
	logout: logout,
}



